﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class RM69350_SK622_raydium : Form
    {
        private bool Listening = false;
        private bool IClosing = false;
        #region 
        CaSDK Ca310Obj = new CaSDK();
        private double auto_lum;
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;
        private ushort[] mygamma_r = new ushort[28];
        private ushort[] mygamma_g = new ushort[28];
        private ushort[] mygamma_b = new ushort[28];
        private string adjust_class = null;
        private void Msr_CA310()
        {

        }
        CaSDK CaSDKObj = new CaSDK();
        private void btnCal_Click(object sender, EventArgs e)   //CA310连接校准
        {
            try
            {
                Tim1Ca310.Start();//显示提示310连接提示信息
                textBox1.Text += "CA310/410 连接校准中...\r\n";
                OLED_Show_Click(btnblack, e);
                Application.DoEvents();//重点，必须加上，否则提示信息不能按顺序显示出来
            }
            catch
            {
                Thread.Sleep(100);
            }
            try
            {
                if (rbt_ca410.Checked == true)
                {
                    CaSDKObj.AutoConnect();
                    CaSDKObj.SingleCalibration(0, byte.Parse(CA310_Channel.Text), double.Parse(CA410_Measure_Freq.Text));
                }
                else
                {
                    CaSDKObj.CA310_Connect();
                    CaSDKObj.CA310_Initial(byte.Parse(CA310_Channel.Text));
                }

                btn_CAS200_Start.Enabled = true;
                CAS200_dataGridView.Enabled = true;
                btn_CAS200_Save.Enabled = true;
                btn_CAS200_MeasureOnce.Enabled = true;
                btn_CAS200_Clean.Enabled = true;
                btn_CAS200_Delect.Enabled = true;
                gBx_CAS200_Auto_Test.Enabled = true;
                btn_CAS200_Stop.Enabled = true;

            }
            catch (Exception EX)
            {
                MessageBox.Show("CA310/410 Connect Fail!!" + EX.Message);
            }

            btnCal_manual.BackColor = Color.Gray;

            //btnAutoGamma.Enabled = true;
            btnAutogammas350.Enabled = true;
            textBox1.Text += "CA310 连接完成...\r\n";


        }
        private void Tim1Ca310_Tick(object sender, EventArgs e)
        {


            Tim1Ca310.Enabled = false;
        }
        private void btn_DisconnectCaX10_Click(object sender, EventArgs e)
        {
            //Ca310Obj.objCa.RemoteMode = 0;
        }
        #endregion

        #region 
        CaSDK Ca41Obj = new CaSDK();
        public double Lv = 0.0;
        public double sx = 0.0;
        public double sy = 0.0;
        #endregion

        private List<byte> buffer = new List<byte>(2048);
        private byte[] Rec_tmp = new byte[1];
        private byte[] Rec_tmp1 = new byte[1];
        private byte[] Send_buf = new byte[1];
        private byte signal_mode;
        public byte start_trans_flag = 0;
        public byte continueflag = 0;
        public byte RM350_0XFE_XX = 0;

        private int step_l = 1;
        private int step_m = 5;
        private int step_h = 12;
        private int adjust_num = 0;



        #region
        frmMain MainForm = null;
        public RM69350_SK622_raydium(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
            // MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);
            MainForm.Show();
            this.Hide();
        }
        private void btnCa410Connect_Click(object sender, EventArgs e)
        {
            //string ca410Info = "";
            //Ca410.AutoConnect();
            //Ca410.DefaultSetting();

            //Ca410.Measurement(ref Lv, ref sx, ref sy, ref ca410Info);
            //textBox1.Text += "Lv " + Lv + "\r\n";
            //textBox1.Text += ca410Info + "\r\n";
        }
        private void btnCal_manual_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnCal_manual, "CA310连接校准，自带黑画面，按下静候6秒");
        }
        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }
        private void btnReload_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            string path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(di);
            textBox2.Text = path;
        }
        private void CreateListItem(DirectoryInfo di)
        {
            foreach (DirectoryInfo childDi in di.GetDirectories())
            {
                this.CreateListItem(childDi);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if (ImageType.Contains(fi.Extension))
                {
                    this.listBox3_BMPFile.Items.Add(new MyImage(fi.FullName));
                }
            }
        }
        private void lstImage_Click(object sender, EventArgs e)
        {
            if (listBox3_BMPFile.SelectedItem == null)
            {
                return;
            }
            MyImage selectedImg = (MyImage)listBox3_BMPFile.SelectedItem;
            Image img = new Bitmap(selectedImg.Path);
            pictureBox1.Image = img;
        }
        System.DateTime TimeNow2 = new DateTime();
        TimeSpan TimeCount2 = new TimeSpan();
        private void btnPrevPic_Click(object sender, EventArgs e)
        {
            MemoryStream aFile;
            aFile = new MemoryStream();
            //FileStream aFile;
            Int32 m_txlen = 0;

            int HACT_True;
            int VACT_True;
            int j;
            TimeNow2 = DateTime.Now; //开始计时

            byte[] TemFileContent = new byte[512 * 4000 + 100];
            int ByteReaded = 0x00;

            string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();

            Bitmap bmp = new Bitmap(FileName);
            bmp.RotateFlip(RotateFlipType.Rotate180FlipX); //Rotate180FlipX，旋转180度        
            bmp.Save(aFile, System.Drawing.Imaging.ImageFormat.Bmp);
            aFile.Seek(0, SeekOrigin.Begin);//           
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);
            if (MainForm.HACT.Value != HACT_True)
            {
                textBox1.Text += "图片宽度与设定不符\r\n";
                return;
            }

            if ((MainForm.Dis_mode.SelectedIndex == 1)) //(Dis_mode.SelectedIndex == 1):MIPI 模式，因为下位机仅对MIPI模式做了优化
            {
                //------------20180903---------------
                try
                {   //bd 5a a5 9DF0-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    //BD A5 5A 9DF0-----提前下个标志位告诉下位机开始 快速 送图了
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0xa5; TemFileContent[2] = 0x5a; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    MainForm.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {

                    j = 0;
                    int x, y, s = 0;
                    x = 512 * 4000; //单次传输数量
                    y = (VACT_True * HACT_True * 3 / x); //传输次数
                    if ((VACT_True * HACT_True * 3 % x) != 0)
                        y++;
                    {
                        //aFile.Seek(54, SeekOrigin.Begin);
                        while (j < y)
                        {
                            //if (HACT_True % 4 != 0)
                            //    aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            //else
                            //    aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                            aFile.Seek((j) * x + 54, SeekOrigin.Begin);

                            ByteReaded = aFile.Read(TemFileContent, 0, x);

                            try
                            {
                                MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }

                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
            else
            {
                //------------20180808---------------
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    MainForm.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 1;
                    //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                    {
                        aFile.Seek(54, SeekOrigin.Begin);
                        while (j <= VACT_True)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);


                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 

                            try
                            {
                                MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            return;
                        }

                        aFile.Close();
                    }
                    /*
                    else
                    {
                        MessageBox.Show("BMP分辨率非法");
                    }*/
                }
                catch
                {
                    textBox1.Text += "啥图片都没显示";
                    textBox1.Select(textBox1.Text.Length, 0);
                    textBox1.ScrollToCaret();
                }
            }


            TimeCount2 = DateTime.Now - TimeNow2;
            textBox1.Text = string.Format("{0:00}:{1:00}", TimeCount2.Seconds, TimeCount2.Milliseconds);
        }
        private void RM69330_Load(object sender, EventArgs e)
        {
            string[] s = { ".bmp" };
            this.ImageType = new List<string>(s);
        }
        private void btnCleantextbox1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
        private void Frm_Activated(object sender, EventArgs e)
        {

        }
        private void RM692Ax_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            btnBack_Click(btnBack, e);
        }
        #endregion
        public byte Seil_port_Find_flag = 0;
        private byte[] Driver_IC = new byte[1];
        private List<string> ImageType;
        private delegate void MonitorSerialPortChangeEventHandler();
        private MonitorSerialPortChangeEventHandler MonitorCom;
        private int serialPortStatus;
        public byte RM2AX_0XFE_XX = 0;
        CRC16 CRCData = new CRC16();
        #region
        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            textBox1.Focus();
            textBox1.Select(textBox1.TextLength, 0);
            textBox1.ScrollToCaret();
        }
        #endregion
        #region  
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            try
            {
                Listening = true;
                int n = MainForm.comPort.BytesToRead;
                byte[] buf = new byte[n];


                MainForm.comPort.Read(buf, 0, n);
                bool data_1_catched = false;
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (buffer[0] == 0x0B)
                    {
                        if (buffer.Count >= (Int16)(buffer[2] << 8) + (buffer[3]) + 7)
                        {
                            Rec_tmp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            byte[] crcTemp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            buffer.CopyTo(0, crcTemp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 2] != CRCH && buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 1] != CRCL)
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);
                                data_1_catched = true;
                                buffer.RemoveRange(0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);
                            }

                        }
                    }
                    else if (buffer[0] == 0xFE)
                    {
                        Rec_tmp = new byte[buffer[2] + 5];
                        byte[] crcTemp = new byte[buffer[2] + 5];
                        buffer.CopyTo(0, crcTemp, 0, buffer[2] + 5);

                        CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                        if (buffer[buffer[2] + 5 - 2] != CRCH && buffer[buffer[2] + 5 - 1] != CRCL)
                        {
                            buffer.RemoveRange(0, buffer.Count);
                        }
                        else
                        {
                            buffer.CopyTo(0, Rec_tmp, 0, buffer[2] + 5);
                            data_1_catched = true;
                            buffer.RemoveRange(0, buffer[2] + 5);
                            if (Rec_tmp[4] == 0)
                                continueflag = 1;
                        }
                    }
                    else if (buffer[0] == 0x55)
                    {
                        if (buffer.Count >= (buffer[2] + 5))
                        {
                            Rec_tmp = new byte[(buffer[2] + 5)];
                            byte[] crcTemp = new byte[(buffer[2] + 5)];
                            buffer.CopyTo(0, crcTemp, 0, (buffer[2] + 5));

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(buffer[2] + 5) - 2] != CRCH && buffer[(buffer[2] + 5) - 1] != CRCL)
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (buffer[2] + 5));
                                data_1_catched = true;
                                buffer.RemoveRange(0, (buffer[2] + 5));
                                switch (Rec_tmp[3])
                                {
                                    case 0x00:
                                        {
                                            start_trans_flag = 1;
                                        }
                                        break;
                                    case 0x01:
                                        {
                                            if (MainForm.comPort.IsOpen == true) MainForm.comPort.Close();
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            start_trans_flag = 0;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }

                        }
                    }

                    else if ((buffer[0] == 0x32) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        T1.Enabled = false;


                        if ((Rec_tmp[0] == 0x09) || (Rec_tmp[0] == 0x19) || (Rec_tmp[0] == 0x29) || (Rec_tmp[0] == 0x2e))
                        {

                        }

                        else if (Rec_tmp[0] == 0x32)
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01: { if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "写寄存器\tOK！\r\n"; } break;


                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00) textBox1.Text = textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x40) textBox1.Text = textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x80) textBox1.Text = textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Gamma2 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Gamma2 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Gamma3 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Gamma3 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写GOA OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写GOA OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x10:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        // textBox9.Text = tmp;
                                        textBox1.Text = textBox1.Text + "OTP\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Others OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Others OTP\t写 ERROR！\r\n";
                                    }
                                    break;

                                default: break;

                            }
                        }


                        else if (Rec_tmp[0] == 0x2F)
                        {
                            if ((Rec_tmp[7] == 0x2E) || (Rec_tmp[7] == 0x59))
                            {
                                if (Rec_tmp[7] == 0x2E)
                                {
                                    textBox1.Text = textBox1.Text + "297 298 299\r\n";
                                }
                                else if (Rec_tmp[7] == 0x59)
                                {
                                    textBox1.Text = textBox1.Text + "350\r\n";
                                }
                                switch (Rec_tmp[1])
                                {
                                    case 0x01:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Enable\r\n";
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Disable\r\n";
                                        }
                                        break;
                                    case 0x03:
                                        {
                                            if (Rec_tmp[7] == 0x2E)
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash complete ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x59)
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash1 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x0A:
                                        {
                                            if (Rec_tmp[7] == 0x59)
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash2 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x04:
                                        {
                                            textBox1.Text = textBox1.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x05:
                                        {
                                            if (Rec_tmp[7] == 0x2E)
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x59)
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x06:
                                        {
                                            textBox1.Text = textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x07:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function on \r\n";
                                        }
                                        break;
                                    case 0x08:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function off \r\n";
                                        }
                                        break;
                                    case 0x09:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura OTP complete$ \r\n";
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }

                        textBox1.Focus();
                        textBox1.Select(textBox1.TextLength, 0);
                        textBox1.ScrollToCaret();
                        if (textBox1.Lines.Length > 200) textBox1.Text = "";
                    }));
                }
            }
            finally
            {
                Listening = false;

            }

        }
        #endregion

        #region
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A;
            Send_buf[2] = 0x15;
            switch (btnTmp.Name)
            {

                case "btnPatternW":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;





                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break;
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break;
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break;
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break;
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break;
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
                numPatternG.Enabled = false;
                numPatternB.Enabled = false;
            }
            else
            {
                numPatternG.Enabled = true;
                numPatternB.Enabled = true;
            }
        }
        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }
        private void numPatternG_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternB.Value = numPatternG.Value;
                numPatternR.Value = numPatternB.Value;
            }
        }
        private void numPatternB_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternB.Value;
                numPatternR.Value = numPatternG.Value;
            }
        }
        private void CAS200_Measure_Information(double Dgv_lum, double Dgv_x, double Dgv_y)
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.CAS200_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.CAS200_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.CAS200_dataGridView.Rows.Add(Row1);
            }
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;

            this.CAS200_dataGridView.Rows[Row_Accum].Cells[1].Value = Dgv_x.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[2].Value = Dgv_y.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[3].Value = Dgv_lum.ToString("#0.0000");
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
        }
        private double R_x = 0.67;
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;
        private double lum_off, x_off, y_off;
        #endregion

        #region 
        Gamma_Addr GAMMAs = new Gamma_Addr();
        private ushort[] gamma_r = new ushort[27];
        private ushort[] gamma_g = new ushort[27];
        private ushort[] gamma_b = new ushort[27];
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;

        #region  
        System.DateTime TimeNow = new DateTime();
        TimeSpan TimeCount = new TimeSpan();
        private void TimTest_Tick(object sender, EventArgs e)
        {

        }
        #endregion
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        private int index = 0;
        private double[,] D_lums;
        private double[] D_lum;
        private double D_x, D_y;
        private bool[] PointCheck = new bool[31];
        private static bool GammaAdjustTimeoutFlag = false;
        private static int GammaAdjustTimes = 0;
        private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue, int lastpoint, int lastRegValue, out int nowRegValueAdj)
        {
            int step_tmp;
            if (nowRegValue > lastRegValue)
            {
                nowRegValue = lastRegValue - 1;
            }
            if (Math.Abs(nowlum - targetlum) > targetlum * 20 / 100)
            {
                if (lastpoint - nowpoint > 6) { step_tmp = 6; }
                else step_tmp = 3;
            }
            else if (Math.Abs(nowlum - targetlum) > targetlum * 10 / 100)
            {
                if (lastpoint - nowpoint > 4) { step_tmp = 6; }
                else step_tmp = 3;
            }
            else
            {
                step_tmp = 1;
            }

            if (nowlum > 0)
            {
                if (nowlum > targetlum)
                    nowRegValueAdj = nowRegValue - step_tmp;
                else nowRegValueAdj = nowRegValue + step_tmp;
            }
            else nowRegValueAdj = lastRegValue - 10;

        }
        byte k = 0;

        bool lumdataReadyFlag;

        #endregion
        #region 
        private byte[] Point_RM69330 = new byte[27] { 0, 1, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 47, 55, 63, 79, 95, 111, 127, 143, 159, 175, 191, 207, 223, 239, 255 };
        private int CAS200_Measure_Gray = 0; //51值
        private int CAS200_Measure_Count = 0;
        private double[] lumRead = new double[31];
        private void T3_Tick(object sender, EventArgs e)
        {

        }
        private void btn_CAS200_Synt_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            switch (btnTmp.Name)
            {
                case "btn_CAS200_MeasureOnce":
                    {
                        Msr_CA310();
                        CAS200_Measure_Information(auto_lum, auto_x, auto_y);
                    }
                    break;
                case "btn_CAS200_Save":
                    {
                        SaveFileDialog sf = new SaveFileDialog();
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*";

                        string str = "序号,x坐标,y坐标,亮度\r\n";
                        if (sf.ShowDialog() == DialogResult.OK)
                        {
                            if (sf.FileName == "") return;
                            try
                            {
                                StreamWriter sw = null;
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i = 0; i < (this.CAS200_dataGridView.Rows.Count - 1); i++)
                                {
                                    str = str + this.CAS200_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[3].Value.ToString() + "\r\n";

                                }
                                sw.WriteLine(str);
                                sw.Dispose();
                                sw.Close();
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Load":
                    {
                        OpenFileDialog sf = new OpenFileDialog();
                        int Line_Count = 0;
                        string strtmp = "";
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*";
                        sf.Title = "数据导入";
                        if (sf.ShowDialog() == DialogResult.OK)
                        {
                            FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                            string strLine = streamReader.ReadLine();
                            while (!string.IsNullOrEmpty(strLine))
                            {
                                strtmp = strtmp + strLine + ",";
                                strLine = streamReader.ReadLine();
                                Line_Count++;
                            }
                            fs.Close();
                            streamReader.Close();
                            string[] arrtmp = strtmp.Split(new Char[] { ',' });
                            for (int j = 1; j < Line_Count - 1; j++)
                            {
                                DataGridViewRow Row1 = new DataGridViewRow();
                                CAS200_dataGridView.Rows.Add(Row1);
                            }
                            try
                            {
                                for (int i = 0; i < Line_Count - 1; i++)
                                {
                                    CAS200_dataGridView.Rows[i].Cells[0].Value = arrtmp[4 * (i + 1) + 0];
                                    CAS200_dataGridView.Rows[i].Cells[1].Value = arrtmp[4 * (i + 1) + 1];
                                    CAS200_dataGridView.Rows[i].Cells[2].Value = arrtmp[4 * (i + 1) + 2];
                                    CAS200_dataGridView.Rows[i].Cells[3].Value = arrtmp[4 * (i + 1) + 3];
                                }
                                MessageBox.Show("CA310数据导入OK！", "提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据导入时发生错误！", "提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Clean":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            this.CAS200_dataGridView.Rows.Clear();
                        }
                        else {; }
                    }
                    break;
                case "btn_CAS200_Delect":
                    {
                        int Current_Index, Row_Accum;
                        Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                        DataGridViewRow Row1 = CAS200_dataGridView.Rows[Current_Index];
                        this.CAS200_dataGridView.Rows.Remove(Row1);
                        this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                        Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                        for (Current_Index = 0; Current_Index < Row_Accum; Current_Index++)
                        {
                            this.CAS200_dataGridView.Rows[Current_Index].Cells[0].Value = Current_Index + 1;
                        }
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
                        CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
                    }
                    break;
                case "btn_CAS200_Stop":
                    {
                        T3.Enabled = false;
                    }
                    break;
                case "btn_CAS200_Start":
                    {
                        CAS200_Measure_Count = 0;//绑点计数

                        CAS200_Measure_Gray = 256;
                        numPatternR.Value = numPatternB.Value = numPatternG.Value = 255;
                        T3.Enabled = true;
                        T3.Interval = int.Parse(CAS200_txt1.Text);
                    }
                    break;
                default: break;
            }
        }
        private void CAS200_MeasureStep_TextChanged(object sender, EventArgs e)
        {
            UInt16 i = Convert.ToUInt16(CAS200_MeasureStep.Text);
            if ((i >= 1) && (i <= 8))
            {
                if ((i == 3) | (i == 5))
                {
                    i = 4;
                }
                else if ((i == 6) | (i == 7))
                {
                    i = 8;
                }
            }
            else
            {
                i = 1;
            }

            CAS200_MeasureStep.Text = i.ToString();
        }
        #endregion
        private void RM692AX_51Set(UInt16 valu51)
        {
            Send_buf = new byte[8];
            Setting.TempRecord.cmdLen = 8;
            valu51 &= 0x0FF;
            Send_buf[0] = 0x3E;
            Send_buf[1] = 0x51;
            Send_buf[2] = 0x03;
            Send_buf[3] = 0x51;
            Send_buf[4] = (Byte)(valu51);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 8);
            }
            catch
            {
                MessageBox.Show("51set串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private static bool Gamma_adjust_timeout_flag = false;
        private static int Gamma_auto_adjust_count = 0;
        int step_tmpr = 32, step_tmpg = 32, step_tmpb = 32;//调节初始步长
        bool fdirr = true;
        bool fdirg = true;
        bool fdirb = true;
        byte nowdirection = 0, lastdirection = 0;
        byte nowdirectionG = 0, lastdirectionG = 0; byte nowdirectionB = 0, lastdirectionB = 0;
        private void RM350_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x59;        //RM69350
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM69350_Reg_R":       //              2
                    {
                        RM350_0XFE_XX = 0x00;
                        Setting.TempRecord.RM69350CmdType = 0x01;   //读普通寄存器    1
                        Send_buf[1] = 0x02;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(R69350_Reg_Addr.Text.Trim(), 16)).ToString());
                    }
                    break;
                case "RM69350_51Reg_W":       //              2
                    {

                        Send_buf[1] = 0x59;     //////////有问题么？？？？？？？？？？？？
                        Send_buf[3] = byte.Parse((Convert.ToInt32(R69350_Reg_Addr.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(R69350_Reg_Data.Text.Trim(), 16)).ToString());
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        MainForm.comPort.Write(Send_buf, 0, 7);
                        return;
                    }
                    break;
                case "RM69350_Reg_W":         //w写寄存器   1
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x01;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(R69350_Reg_Addr.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(R69350_Reg_Data.Text.Trim(), 16)).ToString());
                    }
                    break;
                case "RM69350_R_OTPTime":       //          B
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0B;
                        Setting.TempRecord.RM69350CmdType = 0x02;       //      2
                        if (radioButton17.Checked == true)
                        {
                            Send_buf[3] = 0x00;
                        }
                        else if (radioButton16.Checked == true)
                        {
                            Send_buf[3] = 0x01;
                        }
                        else if (radioButton15.Checked == true)
                        {
                            Send_buf[3] = 0x02;
                        }
                        else if (radioButton14.Checked == true)
                        {
                            Send_buf[3] = 0x03;
                        }
                        else if (radioButton13.Checked == true)
                        {
                            Send_buf[3] = 0x04;
                        }
                        else if (radioButton70.Checked == true)///other
                        {
                            Send_buf[3] = 0x05;
                        }
                        else if (radioButton72.Checked == true)///other
                        {
                            Send_buf[3] = 0x06;
                        }
                        else if (radioButton90.Checked == true)///other
                        {
                            Send_buf[3] = 0x07;
                        }
                        else if (radioButtonA0.Checked == true)///other
                        {
                            Send_buf[3] = 0x08;
                        }
                        else if (radioButtonD0.Checked == true)///other
                        {
                            Send_buf[3] = 0x09;
                        }
                        else if (radioButtonD2.Checked == true)///other
                        {
                            Send_buf[3] = 0x10;
                        }
                        else if (radioButton_10.Checked == true)///other
                        {
                            Send_buf[3] = 0x11;
                        }
                        else if (radioButton_20.Checked == true)///other
                        {
                            Send_buf[3] = 0x12;
                        }
                    }
                    break;
                case "RM69350_W_OTPTime":         //w写寄存器   C
                    {
                        RM350_0XFE_XX = 0x00;
                        if (Convert.ToDecimal(textBox68.Text) > 3)
                        {
                            MessageBox.Show("OTP次数不能大于3次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x0C;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(textBox68.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private void btn350Stop_Click(object sender, EventArgs e)
        {

            T17.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
        }
        private void RM350_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[62];
            Setting.TempRecord.cmdLen = 62;
            Send_buf[2] = 57;
            Send_buf[3] = 0;
            Send_buf[0] = 0x59; //RM69350


            switch (btnTmp.Name)
            {
                case "RM69350_GammaR_Red":
                    {
                        Setting.TempRecord.RM69350CmdType = 0x03;       //      3
                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] |= 0x00;                            //红色red
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[4] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[4] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[4] = 0x02;
                        }
                    }
                    break;
                case "RM69350_GammaR_Green":
                    {
                        Setting.TempRecord.RM69350CmdType = 0x04;   //      4
                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] |= 0x42;                        //绿色 green
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[4] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[4] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[4] = 0x02;
                        }
                    }
                    break;
                case "RM69350_GammaR_Blue": //              9
                    {
                        Setting.TempRecord.RM69350CmdType = 0x05;   //      5
                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] |= 0x81;                        //   blue
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[4] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[4] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[4] = 0x02;
                        }
                    }
                    break;
                //--------------------------------------
                case "RM69350_GammaW_Red":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[3] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[3] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[3] = 0x02;
                        }
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(rm350_R0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(rm350_R0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(rm350_R1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(rm350_R1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(rm350_R2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(rm350_R2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(rm350_R3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(rm350_R3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(rm350_R7.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(rm350_R7.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(rm350_R11.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(rm350_R11.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(rm350_R15.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(rm350_R15.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(rm350_R19.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(rm350_R19.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(rm350_R23.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(rm350_R23.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(rm350_R27.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(rm350_R27.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(rm350_R31.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(rm350_R31.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(rm350_R35.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(rm350_R35.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(rm350_R39.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(rm350_R39.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(rm350_R47.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(rm350_R47.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(rm350_R55.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(rm350_R55.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(rm350_R63.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(rm350_R63.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(rm350_R79.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(rm350_R79.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(rm350_R95.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(rm350_R95.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(rm350_R111.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(rm350_R111.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(rm350_R127.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(rm350_R127.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(rm350_R143.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(rm350_R143.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(rm350_R159.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(rm350_R159.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(rm350_R175.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(rm350_R175.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(rm350_R191.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(rm350_R191.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(rm350_R207.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(rm350_R207.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(rm350_R223.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(rm350_R223.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(rm350_R239.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(rm350_R239.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(rm350_R255.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(rm350_R255.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM69350_GammaW_Green":
                    {
                        Send_buf[1] = 0x08;
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[3] = 0x40;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[3] = 0x41;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[3] = 0x42;
                        }
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(rm350_G0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(rm350_G0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(rm350_G1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(rm350_G1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(rm350_G2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(rm350_G2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(rm350_G3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(rm350_G3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(rm350_G7.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(rm350_G7.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(rm350_G11.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(rm350_G11.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(rm350_G15.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(rm350_G15.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(rm350_G19.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(rm350_G19.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(rm350_G23.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(rm350_G23.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(rm350_G27.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(rm350_G27.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(rm350_G31.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(rm350_G31.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(rm350_G35.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(rm350_G35.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(rm350_G39.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(rm350_G39.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(rm350_G47.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(rm350_G47.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(rm350_G55.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(rm350_G55.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(rm350_G63.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(rm350_G63.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(rm350_G79.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(rm350_G79.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(rm350_G95.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(rm350_G95.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(rm350_G111.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(rm350_G111.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(rm350_G127.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(rm350_G127.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(rm350_G143.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(rm350_G143.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(rm350_G159.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(rm350_G159.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(rm350_G175.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(rm350_G175.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(rm350_G191.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(rm350_G191.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(rm350_G207.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(rm350_G207.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(rm350_G223.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(rm350_G223.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(rm350_G239.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(rm350_G239.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(rm350_G255.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(rm350_G255.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM69350_GammaW_Blue":     //      8
                    {
                        Send_buf[1] = 0x08;
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[3] = 0x80;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[3] = 0x81;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[3] = 0x82;
                        }

                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(rm350_B0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(rm350_B0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(rm350_B1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(rm350_B1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(rm350_B2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(rm350_B2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(rm350_B3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(rm350_B3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(rm350_B7.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(rm350_B7.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(rm350_B11.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(rm350_B11.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(rm350_B15.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(rm350_B15.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(rm350_B19.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(rm350_B19.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(rm350_B23.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(rm350_B23.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(rm350_B27.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(rm350_B27.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(rm350_B31.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(rm350_B31.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(rm350_B35.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(rm350_B35.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(rm350_B39.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(rm350_B39.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(rm350_B47.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(rm350_B47.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(rm350_B55.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(rm350_B55.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(rm350_B63.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(rm350_B63.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(rm350_B79.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(rm350_B79.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(rm350_B95.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(rm350_B95.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(rm350_B111.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(rm350_B111.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(rm350_B127.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(rm350_B127.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(rm350_B143.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(rm350_B143.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(rm350_B159.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(rm350_B159.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(rm350_B175.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(rm350_B175.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(rm350_B191.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(rm350_B191.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(rm350_B207.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(rm350_B207.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(rm350_B223.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(rm350_B223.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(rm350_B239.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(rm350_B239.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(rm350_B255.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(rm350_B255.Value.ToString())) & 0xff);

                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[60], out Send_buf[61]);
            try
            {
                RM350_0XFE_XX = 0x00;
                MainForm.comPort.Write(Send_buf, 0, 62);   //RM69350
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        //RM69350
        private int[] Parttern_RM67350 = new int[27] { 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 3, 2, 1 };
        private byte[] Point_RM69350 = new byte[27] { 1, 2, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 47, 55, 63, 79, 95, 111, 127, 143, 159, 175, 191, 207, 223, 239, 255 };
        private void btnCalculate350_Click(object sender, EventArgs e)
        {
            txt_350_255.Text = (Math.Pow((Parttern_RM67350[0] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_239.Text = (Math.Pow((Parttern_RM67350[1] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_223.Text = (Math.Pow((Parttern_RM67350[2] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_207.Text = (Math.Pow((Parttern_RM67350[3] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_191.Text = (Math.Pow((Parttern_RM67350[4] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_175.Text = (Math.Pow((Parttern_RM67350[5] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_159.Text = (Math.Pow((Parttern_RM67350[6] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_143.Text = (Math.Pow((Parttern_RM67350[7] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_127.Text = (Math.Pow((Parttern_RM67350[8] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_111.Text = (Math.Pow((Parttern_RM67350[9] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_95.Text = (Math.Pow((Parttern_RM67350[10] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_79.Text = (Math.Pow((Parttern_RM67350[11] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_63.Text = (Math.Pow((Parttern_RM67350[12] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_55.Text = (Math.Pow((Parttern_RM67350[13] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_47.Text = (Math.Pow((Parttern_RM67350[14] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_39.Text = (Math.Pow((Parttern_RM67350[15] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_35.Text = (Math.Pow((Parttern_RM67350[16] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_31.Text = (Math.Pow((Parttern_RM67350[17] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_27.Text = (Math.Pow((Parttern_RM67350[18] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_23.Text = (Math.Pow((Parttern_RM67350[19] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_19.Text = (Math.Pow((Parttern_RM67350[20] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_15.Text = (Math.Pow((Parttern_RM67350[21] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_11.Text = (Math.Pow((Parttern_RM67350[22] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_7.Text = (Math.Pow((Parttern_RM67350[23] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_3.Text = (Math.Pow((Parttern_RM67350[24] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_2.Text = (Math.Pow((Parttern_RM67350[25] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_1.Text = (Math.Pow((Parttern_RM67350[26] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
        }
        private void btnAutogamma350_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[27, 3];
            D_lum = new double[27];
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            Button btnTmp = (Button)sender;
            adjust_class = "btnAutogammas350";

            D_lum[0] = Convert.ToDouble(txt_350_255.Text);
            D_lum[1] = Convert.ToDouble(txt_350_239.Text);
            D_lum[2] = Convert.ToDouble(txt_350_223.Text);
            D_lum[3] = Convert.ToDouble(txt_350_207.Text);
            D_lum[4] = Convert.ToDouble(txt_350_191.Text);
            D_lum[5] = Convert.ToDouble(txt_350_175.Text);
            D_lum[6] = Convert.ToDouble(txt_350_159.Text);
            D_lum[7] = Convert.ToDouble(txt_350_143.Text);
            D_lum[8] = Convert.ToDouble(txt_350_127.Text);
            D_lum[9] = Convert.ToDouble(txt_350_111.Text);
            D_lum[10] = Convert.ToDouble(txt_350_95.Text);
            D_lum[11] = Convert.ToDouble(txt_350_79.Text);
            D_lum[12] = Convert.ToDouble(txt_350_63.Text);
            D_lum[13] = Convert.ToDouble(txt_350_55.Text);
            D_lum[14] = Convert.ToDouble(txt_350_47.Text);
            D_lum[15] = Convert.ToDouble(txt_350_39.Text);
            D_lum[16] = Convert.ToDouble(txt_350_35.Text);
            D_lum[17] = Convert.ToDouble(txt_350_31.Text);
            D_lum[18] = Convert.ToDouble(txt_350_27.Text);
            D_lum[19] = Convert.ToDouble(txt_350_23.Text);
            D_lum[20] = Convert.ToDouble(txt_350_19.Text);
            D_lum[21] = Convert.ToDouble(txt_350_15.Text);
            D_lum[22] = Convert.ToDouble(txt_350_11.Text);
            D_lum[23] = Convert.ToDouble(txt_350_7.Text);
            D_lum[24] = Convert.ToDouble(txt_350_3.Text);
            D_lum[25] = Convert.ToDouble(txt_350_2.Text);
            D_lum[26] = Convert.ToDouble(txt_350_1.Text);
            D_x = Convert.ToDouble(txt_350_x.Text);
            D_y = Convert.ToDouble(txt_350_y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 27; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }


            mygamma_r = GAMMAs.RM350_Gamma_r;
            mygamma_g = GAMMAs.RM350_Gamma_g;
            mygamma_b = GAMMAs.RM350_Gamma_b;



            if (adjust_class == "btnAutogammas350")
            {
                R69350_Reg_Addr.Text = "FE";
                R69350_Reg_Data.Text = "51";
                RM350_Others_Click(RM69350_Reg_W, e);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txt_350_loff.Text);
                        x_off = Convert.ToDouble(txt_350_xoff.Text);
                        y_off = Convert.ToDouble(txt_350_yoff.Text);

                        if (SK_102_Check.Checked == true)
                        {
                            R66451_btnPrevPic_Run("W" + Point_RM69350[26].ToString() + ".dsc_0001.bmp");
                            //R66451_btnPrevPic_Run("A0D" + Point_RM69350[26].ToString() + ".dsc_0001.bmp");
                        }
                        else if (SK_102_Check.Checked == false)
                        {
                            OLED_Show_Click(btnwhite, e);
                        }
                        T17.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        byte GammaAdjN_COUNT = 0;
        byte GammaAdjN_COUNTG = 0; byte GammaAdjN_COUNTB = 0;
        //byte k = 0;
        bool array2value = false;   //记录值写入窗体控件(记录绑点RGB寄存器值)的标志位
        bool value2array = true;    //窗体控件值保存到记录标志位
        private void RM350_OTP_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x59;        //RM69350
            Send_buf[2] = 0x02;

            switch (btnTmp.Name)
            {
                case "RM350_OTP_GammaSet1": //otp GammaSet1       A
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0A;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x00;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton17.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0A;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }

                    }
                    break;
                case "RM350_OTP_GammaSet2"://otp GammaSet2        D
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0D;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x01;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton16.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0D;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_GammaSet3"://otp GammaSet3        E
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0E;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x02;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton15.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0E;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_GOA"://otp goa                    F
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0F;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x03;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton14.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0F;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Power"://otp power                10
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x10;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x04;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton13.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x10;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Other_72":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x1B;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x06;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton72.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x1B;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x07;
                    }
                    break;
            }
        }
        private void RM350_other_OTP_click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x59;        //RM69350
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM350_OTP_Other_70":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x1A;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x05;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                        Delay(100);
                        if (radioButton70.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x1A;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Other_90":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x1C;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x07;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                        Delay(100);
                        if (radioButton90.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x1C;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Other_A0":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x1D;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x08;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                        Delay(100);
                        if (radioButtonA0.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x1D;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Other_D0":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x1E;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x09;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                        Delay(100);
                        if (radioButtonD0.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x1E;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Other_D2":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x1F;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x10;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                        Delay(100);
                        if (radioButtonD2.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x1F;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Other_10":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x20;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x11;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                        Delay(100);
                        if (radioButtonD2.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x20;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM350_OTP_Other_20":
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x21;
                        Setting.TempRecord.RM69350CmdType = 0x02;
                        Send_buf[3] = 0x12;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 7);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                        Delay(100);
                        if (radioButtonD2.Checked == true)
                        {
                            RM350_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox68.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[1] = 0x21;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 7);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
            }
        }
        private void radio_Gamma3_CheckedChanged(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x59;
            Send_buf[2] = 0x02;

            RadioButton btnTmp = (RadioButton)sender;
            Setting.TempRecord.RM69350CmdType = 0x06;       //      6
            if (RM350_radioGamma1.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x01;

            }
            else if (RM350_radioGamma2.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x02;
            }
            else if (RM350_radioGamma3.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x03;
            }
            if (radioButton12.Checked == true)
            {
                Send_buf[4] = 0x01;
            }
            else
            {
                Send_buf[4] = 0x02;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                RM350_0XFE_XX = 0x00;
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        byte extraInfo;
        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            R69350_51Reg.Text = RM69350_51Reg_trackBar1.Value.ToString() + "阶";
            if (RM350_0XFE_XX != 0xF0)
            {
                RM350_0XFE_XX = 0xF0;
                R69350_Reg_Addr.Text = "fe";
                R69350_Reg_Data.Text = "00";
                RM350_Others_Click(RM69350_51Reg_W, e);
            }
            R69350_Reg_Addr.Text = "51";
            R69350_Reg_Data.Text = RM69350_51Reg_trackBar1.Value.ToString("x2");
            RM350_Others_Click(RM69350_51Reg_W, e);
        }
        //Demura
        private void btnReload_Demura_File_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件.");
                return;
            }
            string path = fldb.SelectedPath;

            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            listBox3_Demura_File.Items.Clear();
            string s = null;
            DirectoryInfo d = new DirectoryInfo(path);
            FileInfo[] Files = d.GetFiles("*.hex");//*.hex|
            // List<string> lstr = new List<string>();

            foreach (FileInfo file in Files)
            {
                s = file.Name;
                listBox3_Demura_File.Items.Add(s);
            }
            FileInfo[] Files1 = d.GetFiles("*.bin");
            foreach (FileInfo file in Files1)
            {
                s = file.Name;
                listBox3_Demura_File.Items.Add(s);
            }
            textBox5.Text = path;
        }
        private void Demura_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[10];
            Setting.TempRecord.cmdLen = 10;
            Send_buf[0] = 0x2F;
            Send_buf[2] = 0x05;
            Send_buf[3] = 0x13;//0x11:692Ax-6.088;  1:692Ax-KPQ//2:692C4  ------------------------- 等下重点关注一下
            Send_buf[7] = 0x59;//--------------------------------------------69350------------------------------

            switch (btnTmp.Name)
            {
                case "Demura_Function_ON":
                    {
                        Send_buf[1] = 0x07;
                    }
                    break;
                case "Demura_Function_OFF":
                    {
                        Send_buf[1] = 0x08;
                    }
                    break;
                case "Inter_Demura_data":
                    {
                        Send_buf[1] = 0x01;
                    }
                    break;
                case "Exit_Demura_data":
                    {
                        Send_buf[1] = 0x02;
                    }
                    break;
                case "Flash_Write_Demura_data":
                    {
                        Send_buf[1] = 0x03;
                    }
                    break;
                case "Flash_Check_Demura_data":
                    {
                        Send_buf[1] = 0x04;
                    }
                    break;
                case "Flash_Erase_Demura_data":
                    {
                        Send_buf[1] = 0x05;
                    }
                    break;
                case "Demura_Flash_Enable":
                    {
                        Send_buf[1] = 0x06;
                    }
                    break;
                case "Flash_Demura_OTP":
                    {
                        Send_buf[1] = 0x09;
                    }
                    break;
                case "Flash_Write_Demura_data2":
                    {
                        Send_buf[1] = 0x0A;
                    }
                    break;
                case "Flash_Write_Demura_data3":
                    {
                        Send_buf[1] = 0x0F;
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);

            if (Send_buf[1] == 0x03) //烧录前预加载Demura文件
            {
                PreLoad_Demura_data_Click();
            }
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 10);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            if (Send_buf[1] == 0x03)//烧写时给出相应耗时提示
            {
                MessageBoxTimeoutA((IntPtr)0, "烧写预计需要耗时15S，请留意信息窗烧录完成提示...", "---Info---15S后本窗口自动关闭---", 0, 0, 19000);
            }
            else if (Send_buf[1] == 0x05)  //添加擦除等待提示
            {
                MessageBoxTimeoutA((IntPtr)0, "擦除中，大约需要15S，请稍后...", "---Info---15S后本窗口自动关闭---", 0, 0, 19000);// 直接调用  3秒后自动关闭 父窗口句柄没有直接用0代替
            }













            try
            {
                MainForm.comPort.Write(Send_buf, 0, 10);

            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }




        }
        bool gammasetjumpflag = false;//jump 按钮
        private void BtnData2DemuraRam_Click(object sender, EventArgs e)
        {
            PreLoad_Demura_data_Click();
        }

        private void T17_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            Msr_CA310();

            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
                flag1 = 0;
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
                flag2 = 0;
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T17.Enabled = false;
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;

                if (index == Parttern_RM67350.Length)
                {
                    textBox1.Text = textBox1.Text + "RM69350 gamma调试完毕!\r\n";
                }
                if (index < Parttern_RM67350.Length)
                {
                    if ((adjust_class == "btnAutogamma350") && (adjust_num == 1))
                    {
                        switch (index)
                        {
                            case 1:
                                {
                                    rm350_R239.Value = rm350_R255.Value - 16;
                                    rm350_G239.Value = rm350_G255.Value - 16;
                                    rm350_B239.Value = rm350_B255.Value - 16;
                                    lum_off = Convert.ToDouble(txt_350_239off.Text);
                                    auto_gamma_write(mygamma_r[26], Convert.ToUInt16(rm350_R239.Value), mygamma_g[26], Convert.ToUInt16(rm350_G239.Value), mygamma_b[26], Convert.ToUInt16(rm350_B239.Value));
                                }
                                break;
                            case 2:
                                {
                                    rm350_R223.Value = rm350_R239.Value - 16;
                                    rm350_G223.Value = rm350_G239.Value - 16;
                                    rm350_B223.Value = rm350_B239.Value - 16;
                                    lum_off = Convert.ToDouble(txt_350_223off.Text);
                                    auto_gamma_write(mygamma_r[25], Convert.ToUInt16(rm350_R223.Value), mygamma_g[25], Convert.ToUInt16(rm350_G223.Value), mygamma_b[25], Convert.ToUInt16(rm350_B223.Value));
                                }
                                break;
                            case 3:
                                {
                                    rm350_R207.Value = rm350_R223.Value - 16;
                                    rm350_G207.Value = rm350_G223.Value - 16;
                                    rm350_B207.Value = rm350_B223.Value - 16;
                                    lum_off = Convert.ToDouble((txt_350_207off.Text));
                                    auto_gamma_write(mygamma_r[24], Convert.ToUInt16(rm350_R207.Value), mygamma_g[24], Convert.ToUInt16(rm350_G207.Value), mygamma_b[24], Convert.ToUInt16(rm350_B207.Value));
                                }
                                break;
                            case 4:
                                {
                                    rm350_R191.Value = rm350_R207.Value - 16;
                                    rm350_G191.Value = rm350_G207.Value - 16;
                                    rm350_B191.Value = rm350_B207.Value - 16;
                                    lum_off = Convert.ToDouble(txt_350_191off.Text);
                                    auto_gamma_write(mygamma_r[23], Convert.ToUInt16(rm350_R191.Value), mygamma_g[23], Convert.ToUInt16(rm350_G191.Value), mygamma_b[23], Convert.ToUInt16(rm350_B191.Value));
                                }
                                break;
                            case 5:
                                {
                                    rm350_R175.Value = rm350_R191.Value - 32;
                                    rm350_G175.Value = rm350_G191.Value - 32;
                                    rm350_B175.Value = rm350_B191.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_175off.Text);
                                    auto_gamma_write(mygamma_r[22], Convert.ToUInt16(rm350_R175.Value), mygamma_g[22], Convert.ToUInt16(rm350_G175.Value), mygamma_b[22], Convert.ToUInt16(rm350_B175.Value));
                                }
                                break;
                            case 6:
                                {
                                    rm350_R159.Value = rm350_R175.Value - 32;
                                    rm350_G159.Value = rm350_G175.Value - 32;
                                    rm350_B159.Value = rm350_B175.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_159off.Text);
                                    auto_gamma_write(mygamma_r[21], Convert.ToUInt16(rm350_R159.Value), mygamma_g[21], Convert.ToUInt16(rm350_G159.Value), mygamma_b[21], Convert.ToUInt16(rm350_B159.Value));
                                }
                                break;
                            case 7:
                                {
                                    rm350_R143.Value = rm350_R159.Value - 32;
                                    rm350_G143.Value = rm350_G159.Value - 32;
                                    rm350_B143.Value = rm350_B159.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_143off.Text);
                                    auto_gamma_write(mygamma_r[20], Convert.ToUInt16(rm350_R143.Value), mygamma_g[20], Convert.ToUInt16(rm350_G143.Value), mygamma_b[20], Convert.ToUInt16(rm350_B143.Value));
                                }
                                break;
                            case 8:
                                {
                                    rm350_R127.Value = rm350_R143.Value - 32;
                                    rm350_G127.Value = rm350_G143.Value - 32;
                                    rm350_B127.Value = rm350_B143.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_127off.Text);
                                    auto_gamma_write(mygamma_r[19], Convert.ToUInt16(rm350_R127.Value), mygamma_g[19], Convert.ToUInt16(rm350_G127.Value), mygamma_b[19], Convert.ToUInt16(rm350_B127.Value));
                                }
                                break;
                            case 9:
                                {
                                    rm350_R111.Value = rm350_R127.Value - 32;
                                    rm350_G111.Value = rm350_G127.Value - 32;
                                    rm350_B111.Value = rm350_B127.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_111off.Text);
                                    auto_gamma_write(mygamma_r[18], Convert.ToUInt16(rm350_R111.Value), mygamma_g[18], Convert.ToUInt16(rm350_G111.Value), mygamma_b[18], Convert.ToUInt16(rm350_B111.Value));
                                }
                                break;
                            case 10:
                                {
                                    rm350_R95.Value = rm350_R111.Value - 32;
                                    rm350_G95.Value = rm350_G111.Value - 32;
                                    rm350_B95.Value = rm350_B111.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_95off.Text);
                                    auto_gamma_write(mygamma_r[17], Convert.ToUInt16(rm350_R95.Value), mygamma_g[17], Convert.ToUInt16(rm350_G95.Value), mygamma_b[17], Convert.ToUInt16(rm350_B95.Value));
                                }
                                break;
                            case 11:
                                {
                                    rm350_R79.Value = rm350_R95.Value - 32;
                                    rm350_G79.Value = rm350_G95.Value - 32;
                                    rm350_B79.Value = rm350_B95.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_79off.Text);
                                    auto_gamma_write(mygamma_r[16], Convert.ToUInt16(rm350_R79.Value), mygamma_g[16], Convert.ToUInt16(rm350_G79.Value), mygamma_b[16], Convert.ToUInt16(rm350_B79.Value));
                                }
                                break;
                            case 12:
                                {
                                    rm350_R63.Value = rm350_R79.Value - 32;
                                    rm350_G63.Value = rm350_G79.Value - 32;
                                    rm350_B63.Value = rm350_B79.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_63off.Text);
                                    auto_gamma_write(mygamma_r[15], Convert.ToUInt16(rm350_R63.Value), mygamma_g[15], Convert.ToUInt16(rm350_G63.Value), mygamma_b[15], Convert.ToUInt16(rm350_B63.Value));
                                }
                                break;
                            case 13:
                                {
                                    rm350_R55.Value = rm350_R63.Value - 32;
                                    rm350_G55.Value = rm350_G63.Value - 32;
                                    rm350_B55.Value = rm350_B63.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_55off.Text);
                                    auto_gamma_write(mygamma_r[14], Convert.ToUInt16(rm350_R55.Value), mygamma_g[14], Convert.ToUInt16(rm350_G55.Value), mygamma_b[14], Convert.ToUInt16(rm350_B55.Value));
                                }
                                break;
                            case 14:
                                {
                                    rm350_R47.Value = rm350_R55.Value - 32;
                                    rm350_G47.Value = rm350_G55.Value - 32;
                                    rm350_B47.Value = rm350_B55.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_47off.Text);
                                    auto_gamma_write(mygamma_r[13], Convert.ToUInt16(rm350_R47.Value), mygamma_g[13], Convert.ToUInt16(rm350_G47.Value), mygamma_b[14], Convert.ToUInt16(rm350_B47.Value));
                                }
                                break;
                            case 15:
                                {
                                    rm350_R39.Value = rm350_R47.Value - 32;
                                    rm350_G39.Value = rm350_G47.Value - 32;
                                    rm350_B39.Value = rm350_B47.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_39off.Text);
                                    auto_gamma_write(mygamma_r[12], Convert.ToUInt16(rm350_R39.Value), mygamma_g[12], Convert.ToUInt16(rm350_G39.Value), mygamma_b[12], Convert.ToUInt16(rm350_B39.Value));
                                }
                                break;
                            case 16:
                                {
                                    rm350_R35.Value = rm350_R39.Value - 32;
                                    rm350_G35.Value = rm350_G39.Value - 32;
                                    rm350_B35.Value = rm350_B39.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_35off.Text);
                                    auto_gamma_write(mygamma_r[11], Convert.ToUInt16(rm350_R35.Value), mygamma_g[11], Convert.ToUInt16(rm350_G35.Value), mygamma_b[11], Convert.ToUInt16(rm350_B35.Value));
                                }
                                break;
                            case 17:
                                {
                                    rm350_R31.Value = rm350_R35.Value - 32;
                                    rm350_G31.Value = rm350_G35.Value - 32;
                                    rm350_B31.Value = rm350_B35.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_31off.Text);
                                    auto_gamma_write(mygamma_r[10], Convert.ToUInt16(rm350_R31.Value), mygamma_g[10], Convert.ToUInt16(rm350_G31.Value), mygamma_b[10], Convert.ToUInt16(rm350_B31.Value));
                                }
                                break;
                            case 18:
                                {
                                    rm350_R27.Value = rm350_R31.Value - 32;
                                    rm350_G27.Value = rm350_G31.Value - 32;
                                    rm350_B27.Value = rm350_B31.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_27off.Text);
                                    auto_gamma_write(mygamma_r[9], Convert.ToUInt16(rm350_R27.Value), mygamma_g[9], Convert.ToUInt16(rm350_G27.Value), mygamma_b[9], Convert.ToUInt16(rm350_B27.Value));
                                }
                                break;
                            case 19:
                                {
                                    rm350_R23.Value = rm350_R27.Value - 32;
                                    rm350_G23.Value = rm350_G27.Value - 32;
                                    rm350_B23.Value = rm350_B27.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_23off.Text);
                                    auto_gamma_write(mygamma_r[8], Convert.ToUInt16(rm350_R23.Value), mygamma_g[8], Convert.ToUInt16(rm350_G23.Value), mygamma_b[8], Convert.ToUInt16(rm350_B23.Value));
                                }
                                break;
                            case 20:
                                {
                                    rm350_R19.Value = rm350_R23.Value - 32;
                                    rm350_G19.Value = rm350_G23.Value - 32;
                                    rm350_B19.Value = rm350_B23.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_19off.Text);
                                    auto_gamma_write(mygamma_r[7], Convert.ToUInt16(rm350_R19.Value), mygamma_g[7], Convert.ToUInt16(rm350_G19.Value), mygamma_b[7], Convert.ToUInt16(rm350_B19.Value));
                                }
                                break;
                            case 21:
                                {
                                    rm350_R15.Value = rm350_R19.Value - 32;
                                    rm350_G15.Value = rm350_G19.Value - 32;
                                    rm350_B15.Value = rm350_B19.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_15off.Text);
                                    auto_gamma_write(mygamma_r[6], Convert.ToUInt16(rm350_R15.Value), mygamma_g[6], Convert.ToUInt16(rm350_G15.Value), mygamma_b[6], Convert.ToUInt16(rm350_B15.Value));
                                }
                                break;
                            case 22:
                                {
                                    rm350_R11.Value = rm350_R15.Value - 32;
                                    rm350_G11.Value = rm350_G15.Value - 32;
                                    rm350_B11.Value = rm350_B15.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_11off.Text);
                                    auto_gamma_write(mygamma_r[5], Convert.ToUInt16(rm350_R11.Value), mygamma_g[5], Convert.ToUInt16(rm350_G11.Value), mygamma_b[5], Convert.ToUInt16(rm350_B11.Value));
                                }
                                break;
                            case 23:
                                {
                                    rm350_R7.Value = rm350_R11.Value - 32;
                                    rm350_G7.Value = rm350_G11.Value - 32;
                                    rm350_B7.Value = rm350_B11.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_7off.Text);
                                    auto_gamma_write(mygamma_r[4], Convert.ToUInt16(rm350_R7.Value), mygamma_g[4], Convert.ToUInt16(rm350_G7.Value), mygamma_b[4], Convert.ToUInt16(rm350_B7.Value));
                                }
                                break;
                            case 24:
                                {
                                    rm350_R3.Value = rm350_R7.Value - 32;
                                    rm350_G3.Value = rm350_G7.Value - 32;
                                    rm350_B3.Value = rm350_B7.Value - 32;
                                    lum_off = Convert.ToDouble(txt_350_3off.Text);
                                    auto_gamma_write(mygamma_r[3], Convert.ToUInt16(rm350_R3.Value), mygamma_g[3], Convert.ToUInt16(rm350_G3.Value), mygamma_b[3], Convert.ToUInt16(rm350_B3.Value));
                                }
                                break;

                        }
                    }
                    else
                    {
                        switch (index)
                        {
                            case 1:
                                lum_off = Convert.ToDouble(txt_350_239off.Text);
                                break;
                            case 2:
                                lum_off = Convert.ToDouble(txt_350_223off.Text);
                                break;
                            case 3:
                                lum_off = Convert.ToDouble(txt_350_207off.Text);
                                break;
                            case 4:
                                lum_off = Convert.ToDouble(txt_350_191off.Text);
                                break;
                            case 5:
                                lum_off = Convert.ToDouble(txt_350_175off.Text);
                                break;
                            case 6:
                                lum_off = Convert.ToDouble(txt_350_159off.Text);
                                break;
                            case 7:
                                lum_off = Convert.ToDouble(txt_350_143off.Text);
                                break;
                            case 8:
                                lum_off = Convert.ToDouble(txt_350_127off.Text) * Convert.ToDouble(txt_350_127.Text) / 100;
                                break;
                            case 9:
                                lum_off = Convert.ToDouble(txt_350_111off.Text) * Convert.ToDouble(txt_350_111.Text) / 100;
                                break;
                            case 10:
                                lum_off = Convert.ToDouble(txt_350_95off.Text) * Convert.ToDouble(txt_350_95.Text) / 100;
                                break;
                            case 11:
                                lum_off = Convert.ToDouble(txt_350_79off.Text) * Convert.ToDouble(txt_350_79.Text) / 100;
                                break;
                            case 12:
                                lum_off = Convert.ToDouble(txt_350_63off.Text) * Convert.ToDouble(txt_350_63.Text) / 100;
                                break;
                            case 13:
                                lum_off = Convert.ToDouble(txt_350_55off.Text) * Convert.ToDouble(txt_350_55.Text) / 100;
                                break;
                            case 14:
                                lum_off = Convert.ToDouble(txt_350_47off.Text) * Convert.ToDouble(txt_350_47.Text) / 100;
                                break;
                            case 15:
                                lum_off = Convert.ToDouble(txt_350_39off.Text) * Convert.ToDouble(txt_350_39.Text) / 100;
                                break;
                            case 16:
                                lum_off = Convert.ToDouble(txt_350_35off.Text) * Convert.ToDouble(txt_350_35.Text) / 100;
                                break;
                            case 17:
                                lum_off = Convert.ToDouble(txt_350_31off.Text) * Convert.ToDouble(txt_350_31.Text) / 100;
                                break;
                            case 18:
                                lum_off = Convert.ToDouble(txt_350_27off.Text) * Convert.ToDouble(txt_350_27.Text) / 100;
                                break;
                            case 19:
                                lum_off = Convert.ToDouble(txt_350_23off.Text) * Convert.ToDouble(txt_350_23.Text) / 100;
                                break;
                            case 20:
                                lum_off = Convert.ToDouble(txt_350_19off.Text) * Convert.ToDouble(txt_350_19.Text) / 100;
                                break;
                            case 21:
                                {
                                    lum_off = Convert.ToDouble(txt_350_15off.Text) * Convert.ToDouble(txt_350_15.Text) / 100;
                                    x_off = x_off + 0.001;
                                    y_off = y_off + 0.001;
                                }
                                break;
                            case 22:
                                {
                                    lum_off = Convert.ToDouble(txt_350_11off.Text) * Convert.ToDouble(txt_350_11.Text) / 100;
                                    x_off = x_off + 0.001;
                                    y_off = y_off + 0.001;
                                }
                                break;
                            case 23:
                                {
                                    lum_off = Convert.ToDouble(txt_350_7off.Text) * Convert.ToDouble(txt_350_7.Text) / 100;
                                    x_off = x_off + 0.001;
                                    y_off = y_off + 0.001;
                                }
                                break;
                            case 24:
                                {
                                    lum_off = Convert.ToDouble(txt_350_3off.Text) * Convert.ToDouble(txt_350_3.Text) / 100;
                                    x_off = x_off + 0.001;
                                    y_off = y_off + 0.001;
                                }
                                break;
                            case 25:
                                {
                                    lum_off = Convert.ToDouble(txt_350_2off.Text) * Convert.ToDouble(txt_350_2.Text) / 100;
                                    x_off = x_off + 0.001;
                                    y_off = y_off + 0.001;
                                }
                                break;
                            case 26:
                                {
                                    lum_off = Convert.ToDouble(txt_350_1off.Text) * Convert.ToDouble(txt_350_1.Text) / 100;
                                    x_off = x_off + 0.001;
                                    y_off = y_off + 0.001;
                                }
                                break;
                        }
                    }

                    numPatternR.Value = Parttern_RM67350[index];
                    numPatternG.Value = Parttern_RM67350[index];
                    numPatternB.Value = Parttern_RM67350[index];
                    if (SK_102_Check.Checked == true)//载入vesa图片
                    {
                        switch (index)
                        {
                            case 1: R66451_btnPrevPic_Run("W" + Point_RM69350[25].ToString() + ".dsc_0001.bmp"); break;
                            case 2: R66451_btnPrevPic_Run("W" + Point_RM69350[24].ToString() + ".dsc_0001.bmp"); break;
                            case 3: R66451_btnPrevPic_Run("W" + Point_RM69350[23].ToString() + ".dsc_0001.bmp"); break;
                            case 4: R66451_btnPrevPic_Run("W" + Point_RM69350[22].ToString() + ".dsc_0001.bmp"); break;
                            case 5: R66451_btnPrevPic_Run("W" + Point_RM69350[21].ToString() + ".dsc_0001.bmp"); break;
                            case 6: R66451_btnPrevPic_Run("W" + Point_RM69350[20].ToString() + ".dsc_0001.bmp"); break;
                            case 7: R66451_btnPrevPic_Run("W" + Point_RM69350[19].ToString() + ".dsc_0001.bmp"); break;
                            case 8: R66451_btnPrevPic_Run("W" + Point_RM69350[18].ToString() + ".dsc_0001.bmp"); break;
                            case 9: R66451_btnPrevPic_Run("W" + Point_RM69350[17].ToString() + ".dsc_0001.bmp"); break;
                            case 10: R66451_btnPrevPic_Run("W" + Point_RM69350[16].ToString() + ".dsc_0001.bmp"); break;
                            case 11: R66451_btnPrevPic_Run("W" + Point_RM69350[15].ToString() + ".dsc_0001.bmp"); break;
                            case 12: R66451_btnPrevPic_Run("W" + Point_RM69350[14].ToString() + ".dsc_0001.bmp"); break;
                            case 13: R66451_btnPrevPic_Run("W" + Point_RM69350[13].ToString() + ".dsc_0001.bmp"); break;
                            case 14: R66451_btnPrevPic_Run("W" + Point_RM69350[12].ToString() + ".dsc_0001.bmp"); break;
                            case 15: R66451_btnPrevPic_Run("W" + Point_RM69350[11].ToString() + ".dsc_0001.bmp"); break;
                            case 16: R66451_btnPrevPic_Run("W" + Point_RM69350[10].ToString() + ".dsc_0001.bmp"); break;
                            case 17: R66451_btnPrevPic_Run("W" + Point_RM69350[9].ToString() + ".dsc_0001.bmp"); break;
                            case 18: R66451_btnPrevPic_Run("W" + Point_RM69350[8].ToString() + ".dsc_0001.bmp"); break;
                            case 19: R66451_btnPrevPic_Run("W" + Point_RM69350[7].ToString() + ".dsc_0001.bmp"); break;
                            case 20: R66451_btnPrevPic_Run("W" + Point_RM69350[6].ToString() + ".dsc_0001.bmp"); break;
                            case 21: R66451_btnPrevPic_Run("W" + Point_RM69350[5].ToString() + ".dsc_0001.bmp"); break;
                            case 22: R66451_btnPrevPic_Run("W" + Point_RM69350[4].ToString() + ".dsc_0001.bmp"); break;
                            case 23: R66451_btnPrevPic_Run("W" + Point_RM69350[3].ToString() + ".dsc_0001.bmp"); break;
                            case 24: R66451_btnPrevPic_Run("W" + Point_RM69350[2].ToString() + ".dsc_0001.bmp"); break;
                            case 25: R66451_btnPrevPic_Run("W" + Point_RM69350[1].ToString() + ".dsc_0001.bmp"); break;
                            case 26: R66451_btnPrevPic_Run("W" + Point_RM69350[0].ToString() + ".dsc_0001.bmp"); break;
                        }
                    }
                    else
                    {
                        OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                    }
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T17.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);



                decimal r, g, b;
                int flexible_gamma_count = 350;

                switch (index)
                {
                    case 0:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R255.Value = rm350_R255.Value - step_h;
                                    else rm350_R255.Value = rm350_R255.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R255.Value = rm350_R255.Value - step_l;
                                    else rm350_R255.Value = rm350_R255.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G255.Value = rm350_G255.Value - step_h;
                                    else rm350_G255.Value = rm350_G255.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G255.Value = rm350_G255.Value - step_l;
                                    else rm350_G255.Value = rm350_G255.Value + step_l;
                                }
                                adjust_Gnum++;

                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B255.Value = rm350_B255.Value - step_h;
                                    else rm350_B255.Value = rm350_B255.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B255.Value = rm350_B255.Value - step_l;
                                    else rm350_B255.Value = rm350_B255.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[27], Convert.ToUInt16(rm350_R255.Value), mygamma_g[27], Convert.ToUInt16(rm350_G255.Value), mygamma_b[27], Convert.ToUInt16(rm350_B255.Value));
                        }
                        break;
                    case 1:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R239.Value = rm350_R239.Value - step_h;
                                    else rm350_R239.Value = rm350_R239.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R239.Value = rm350_R239.Value - step_l;
                                    else rm350_R239.Value = rm350_R239.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G239.Value = rm350_G239.Value - step_h;
                                    else rm350_G239.Value = rm350_G239.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G239.Value = rm350_G239.Value - step_l;
                                    else rm350_G239.Value = rm350_G239.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B239.Value = rm350_B239.Value - step_h;
                                    else rm350_B239.Value = rm350_B239.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B239.Value = rm350_B239.Value - step_l;
                                    else rm350_B239.Value = rm350_B239.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[26], Convert.ToUInt16(rm350_R239.Value), mygamma_g[26], Convert.ToUInt16(rm350_G239.Value), mygamma_b[26], Convert.ToUInt16(rm350_B239.Value));
                        }
                        break;
                    case 2:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R223.Value = rm350_R223.Value - step_h;
                                    else rm350_R223.Value = rm350_R223.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R223.Value = rm350_R223.Value - step_l;
                                    else rm350_R223.Value = rm350_R223.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G223.Value = rm350_G223.Value - step_h;
                                    else rm350_G223.Value = rm350_G223.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G223.Value = rm350_G223.Value - step_l;
                                    else rm350_G223.Value = rm350_G223.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B223.Value = rm350_B223.Value - step_h;
                                    else rm350_B223.Value = rm350_B223.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B223.Value = rm350_B223.Value - step_l;
                                    else rm350_B223.Value = rm350_B223.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[25], Convert.ToUInt16(rm350_R223.Value), mygamma_g[25], Convert.ToUInt16(rm350_G223.Value), mygamma_b[25], Convert.ToUInt16(rm350_B223.Value));
                        }
                        break;
                    case 3:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R207.Value = rm350_R207.Value - step_h;
                                    else rm350_R207.Value = rm350_R207.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R207.Value = rm350_R207.Value - step_l;
                                    else rm350_R207.Value = rm350_R207.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G207.Value = rm350_G207.Value - step_h;
                                    else rm350_G207.Value = rm350_G207.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G207.Value = rm350_G207.Value - step_l;
                                    else rm350_G207.Value = rm350_G207.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B207.Value = rm350_B207.Value - step_h;
                                    else rm350_B207.Value = rm350_B207.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B207.Value = rm350_B207.Value - step_l;
                                    else rm350_B207.Value = rm350_B207.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[24], Convert.ToUInt16(rm350_R207.Value), mygamma_g[24], Convert.ToUInt16(rm350_G207.Value), mygamma_b[24], Convert.ToUInt16(rm350_B207.Value));
                        }
                        break;
                    case 4:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R191.Value = rm350_R191.Value - step_h;
                                    else rm350_R191.Value = rm350_R191.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R191.Value = rm350_R191.Value - step_l;
                                    else rm350_R191.Value = rm350_R191.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G191.Value = rm350_G191.Value - step_h;
                                    else rm350_G191.Value = rm350_G191.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G191.Value = rm350_G191.Value - step_l;
                                    else rm350_G191.Value = rm350_G191.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B191.Value = rm350_B191.Value - step_h;
                                    else rm350_B191.Value = rm350_B191.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B191.Value = rm350_B191.Value - step_l;
                                    else rm350_B191.Value = rm350_B191.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[23], Convert.ToUInt16(rm350_R191.Value), mygamma_g[23], Convert.ToUInt16(rm350_G191.Value), mygamma_b[23], Convert.ToUInt16(rm350_B191.Value));
                        }
                        break;
                    case 5:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R175.Value = rm350_R175.Value - step_h;
                                    else rm350_R175.Value = rm350_R175.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R175.Value = rm350_R175.Value - step_l;
                                    else rm350_R175.Value = rm350_R175.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G175.Value = rm350_G175.Value - step_h;
                                    else rm350_G175.Value = rm350_G175.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_R175.Value = rm350_R175.Value - step_l;
                                    else rm350_R175.Value = rm350_R175.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B175.Value = rm350_B175.Value - step_h;
                                    else rm350_B175.Value = rm350_B175.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B175.Value = rm350_B175.Value - step_l;
                                    else rm350_B175.Value = rm350_B175.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[22], Convert.ToUInt16(rm350_R175.Value), mygamma_g[22], Convert.ToUInt16(rm350_G175.Value), mygamma_b[22], Convert.ToUInt16(rm350_B175.Value));
                        }
                        break;
                    case 6:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R159.Value = rm350_R159.Value - step_h;
                                    else rm350_R159.Value = rm350_R159.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R159.Value = rm350_R159.Value - step_l;
                                    else rm350_R159.Value = rm350_R159.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G159.Value = rm350_G159.Value - step_h;
                                    else rm350_G159.Value = rm350_G159.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G159.Value = rm350_G159.Value - step_l;
                                    else rm350_G159.Value = rm350_G159.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B159.Value = rm350_B159.Value - step_h;
                                    else rm350_B159.Value = rm350_B159.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B159.Value = rm350_B159.Value - step_l;
                                    else rm350_B159.Value = rm350_B159.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[21], Convert.ToUInt16(rm350_R159.Value), mygamma_g[21], Convert.ToUInt16(rm350_G159.Value), mygamma_b[21], Convert.ToUInt16(rm350_B159.Value));
                        }
                        break;
                    case 7:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R143.Value = rm350_R143.Value - step_h;
                                    else rm350_R143.Value = rm350_R143.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R143.Value = rm350_R143.Value - step_l;
                                    else rm350_R143.Value = rm350_R143.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G143.Value = rm350_G143.Value - step_h;
                                    else rm350_G143.Value = rm350_G143.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G143.Value = rm350_G143.Value - step_l;
                                    else rm350_G143.Value = rm350_G143.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B143.Value = rm350_B143.Value - step_h;
                                    else rm350_B143.Value = rm350_B143.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B143.Value = rm350_B143.Value - step_l;
                                    else rm350_B143.Value = rm350_B143.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[20], Convert.ToUInt16(rm350_R143.Value), mygamma_g[20], Convert.ToUInt16(rm350_G143.Value), mygamma_b[20], Convert.ToUInt16(rm350_B143.Value));
                        }
                        break;
                    case 8:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R127.Value = rm350_R127.Value - step_h;
                                    else rm350_R127.Value = rm350_R127.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R127.Value = rm350_R127.Value - step_l;
                                    else rm350_R127.Value = rm350_R127.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G127.Value = rm350_G127.Value - step_h;
                                    else rm350_G127.Value = rm350_G127.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G127.Value = rm350_G127.Value - step_l;
                                    else rm350_G127.Value = rm350_G127.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B127.Value = rm350_B127.Value - step_h;
                                    else rm350_B127.Value = rm350_B127.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B127.Value = rm350_B127.Value - step_l;
                                    else rm350_B127.Value = rm350_B127.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[19], Convert.ToUInt16(rm350_R127.Value), mygamma_g[19], Convert.ToUInt16(rm350_G127.Value), mygamma_b[19], Convert.ToUInt16(rm350_B127.Value));
                        }
                        break;
                    case 9:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R111.Value = rm350_R111.Value - step_h;
                                    else rm350_R111.Value = rm350_R111.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R111.Value = rm350_R111.Value - step_l;
                                    else rm350_R111.Value = rm350_R111.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G111.Value = rm350_G111.Value - step_h;
                                    else rm350_G111.Value = rm350_G111.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G111.Value = rm350_G111.Value - step_l;
                                    else rm350_G111.Value = rm350_G111.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B111.Value = rm350_B111.Value - step_h;
                                    else rm350_B111.Value = rm350_B111.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B111.Value = rm350_B111.Value - step_l;
                                    else rm350_B111.Value = rm350_B111.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[18], Convert.ToUInt16(rm350_R111.Value), mygamma_g[18], Convert.ToUInt16(rm350_G111.Value), mygamma_b[18], Convert.ToUInt16(rm350_B111.Value));
                        }
                        break;
                    case 10:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R95.Value = rm350_R95.Value - step_h;
                                    else rm350_R95.Value = rm350_R95.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R95.Value = rm350_R95.Value - step_l;
                                    else rm350_R95.Value = rm350_R95.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G95.Value = rm350_G95.Value - step_h;
                                    else rm350_G95.Value = rm350_G95.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G95.Value = rm350_G95.Value - step_l;
                                    else rm350_G95.Value = rm350_G95.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B95.Value = rm350_B95.Value - step_h;
                                    else rm350_B95.Value = rm350_B95.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B95.Value = rm350_B95.Value - step_l;
                                    else rm350_B95.Value = rm350_B95.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[17], Convert.ToUInt16(rm350_R95.Value), mygamma_g[17], Convert.ToUInt16(rm350_G95.Value), mygamma_b[17], Convert.ToUInt16(rm350_B95.Value));
                        }
                        break;
                    case 11:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R79.Value = rm350_R79.Value - step_h;
                                    else rm350_R79.Value = rm350_R79.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R79.Value = rm350_R79.Value - step_l;
                                    else rm350_R79.Value = rm350_R79.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G79.Value = rm350_G79.Value - step_h;
                                    else rm350_G79.Value = rm350_G79.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G79.Value = rm350_G79.Value - step_l;
                                    else rm350_G79.Value = rm350_G79.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B79.Value = rm350_B79.Value - step_h;
                                    else rm350_B79.Value = rm350_B79.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B79.Value = rm350_B79.Value - step_l;
                                    else rm350_B79.Value = rm350_B79.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[16], Convert.ToUInt16(rm350_R79.Value), mygamma_g[16], Convert.ToUInt16(rm350_G79.Value), mygamma_b[16], Convert.ToUInt16(rm350_B79.Value));
                        }
                        break;
                    case 12:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R63.Value = rm350_R63.Value - step_h;
                                    else rm350_R63.Value = rm350_R63.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R63.Value = rm350_R63.Value - step_l;
                                    else rm350_R63.Value = rm350_R63.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G63.Value = rm350_G63.Value - step_h;
                                    else rm350_G63.Value = rm350_G63.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G63.Value = rm350_G63.Value - step_l;
                                    else rm350_G63.Value = rm350_G63.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B63.Value = rm350_B63.Value - step_h;
                                    else rm350_B63.Value = rm350_B63.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B63.Value = rm350_B63.Value - step_l;
                                    else rm350_B63.Value = rm350_B63.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[15], Convert.ToUInt16(rm350_R63.Value), mygamma_g[15], Convert.ToUInt16(rm350_G63.Value), mygamma_b[15], Convert.ToUInt16(rm350_B63.Value));
                        }
                        break;
                    case 13:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R55.Value = rm350_R55.Value - step_h;
                                    else rm350_R55.Value = rm350_R55.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R55.Value = rm350_R55.Value - step_l;
                                    else rm350_R55.Value = rm350_R55.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G55.Value = rm350_G55.Value - step_h;
                                    else rm350_G55.Value = rm350_G55.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G55.Value = rm350_G55.Value - step_l;
                                    else rm350_G55.Value = rm350_G55.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B55.Value = rm350_B55.Value - step_h;
                                    else rm350_B55.Value = rm350_B55.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B55.Value = rm350_B55.Value - step_l;
                                    else rm350_B55.Value = rm350_B55.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[14], Convert.ToUInt16(rm350_R55.Value), mygamma_g[14], Convert.ToUInt16(rm350_G55.Value), mygamma_b[14], Convert.ToUInt16(rm350_B55.Value));
                        }
                        break;
                    case 14:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R47.Value = rm350_R47.Value - step_h;
                                    else rm350_R47.Value = rm350_R47.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R47.Value = rm350_R47.Value - step_l;
                                    else rm350_R47.Value = rm350_R47.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G47.Value = rm350_G47.Value - step_h;
                                    else rm350_G47.Value = rm350_G47.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G47.Value = rm350_G47.Value - step_l;
                                    else rm350_G47.Value = rm350_G47.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B47.Value = rm350_B47.Value - step_h;
                                    else rm350_B47.Value = rm350_B47.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B47.Value = rm350_B47.Value - step_l;
                                    else rm350_B47.Value = rm350_B47.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[13], Convert.ToUInt16(rm350_R47.Value), mygamma_g[13], Convert.ToUInt16(rm350_G47.Value), mygamma_b[13], Convert.ToUInt16(rm350_B47.Value));
                        }
                        break;
                    case 15:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R39.Value = rm350_R39.Value - step_h;
                                    else rm350_R39.Value = rm350_R39.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R39.Value = rm350_R39.Value - step_l;
                                    else rm350_R39.Value = rm350_R39.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G39.Value = rm350_G39.Value - step_h;
                                    else rm350_G39.Value = rm350_G39.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G39.Value = rm350_G39.Value - step_l;
                                    else rm350_G39.Value = rm350_G39.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B39.Value = rm350_B39.Value - step_h;
                                    else rm350_B39.Value = rm350_B39.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B39.Value = rm350_B39.Value - step_l;
                                    else rm350_B39.Value = rm350_B39.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[12], Convert.ToUInt16(rm350_R39.Value), mygamma_g[12], Convert.ToUInt16(rm350_G39.Value), mygamma_b[12], Convert.ToUInt16(rm350_B39.Value));
                        }
                        break;
                    case 16:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R35.Value = rm350_R35.Value - step_h;
                                    else rm350_R35.Value = rm350_R35.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R35.Value = rm350_R35.Value - step_l;
                                    else rm350_R35.Value = rm350_R35.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G35.Value = rm350_G35.Value - step_h;
                                    else rm350_G35.Value = rm350_G35.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G35.Value = rm350_G35.Value - step_l;
                                    else rm350_G35.Value = rm350_G35.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B35.Value = rm350_B35.Value - step_h;
                                    else rm350_B35.Value = rm350_B35.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B35.Value = rm350_B35.Value - step_l;
                                    else rm350_B35.Value = rm350_B35.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[11], Convert.ToUInt16(rm350_R35.Value), mygamma_g[11], Convert.ToUInt16(rm350_G35.Value), mygamma_b[11], Convert.ToUInt16(rm350_B35.Value));
                        }
                        break;
                    case 17:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R31.Value = rm350_R31.Value - step_h;
                                    else rm350_R31.Value = rm350_R31.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R31.Value = rm350_R31.Value - step_l;
                                    else rm350_R31.Value = rm350_R31.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G31.Value = rm350_G31.Value - step_h;
                                    else rm350_G31.Value = rm350_G31.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G31.Value = rm350_G31.Value - step_l;
                                    else rm350_G31.Value = rm350_G31.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B31.Value = rm350_B31.Value - step_h;
                                    else rm350_B31.Value = rm350_B31.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B31.Value = rm350_B31.Value - step_l;
                                    else rm350_B31.Value = rm350_B31.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[10], Convert.ToUInt16(rm350_R31.Value), mygamma_g[10], Convert.ToUInt16(rm350_G31.Value), mygamma_b[10], Convert.ToUInt16(rm350_B31.Value));
                        }
                        break;
                    case 18:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R27.Value = rm350_R27.Value - step_h;
                                    else rm350_R27.Value = rm350_R27.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R27.Value = rm350_R27.Value - step_l;
                                    else rm350_R27.Value = rm350_R27.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G27.Value = rm350_G27.Value - step_h;
                                    else rm350_G27.Value = rm350_G27.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G27.Value = rm350_G27.Value - step_l;
                                    else rm350_G27.Value = rm350_G27.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B27.Value = rm350_B27.Value - step_h;
                                    else rm350_B27.Value = rm350_B27.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B27.Value = rm350_B27.Value - step_l;
                                    else rm350_B27.Value = rm350_B27.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[9], Convert.ToUInt16(rm350_R27.Value), mygamma_g[9], Convert.ToUInt16(rm350_G27.Value), mygamma_b[9], Convert.ToUInt16(rm350_B27.Value));
                        }
                        break;
                    case 19:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R23.Value = rm350_R23.Value - step_h;
                                    else rm350_R23.Value = rm350_R23.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R23.Value = rm350_R23.Value - step_l;
                                    else rm350_R23.Value = rm350_R23.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G23.Value = rm350_G23.Value - step_h;
                                    else rm350_G23.Value = rm350_G23.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G23.Value = rm350_G23.Value - step_l;
                                    else rm350_G23.Value = rm350_G23.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B23.Value = rm350_B23.Value - step_h;
                                    else rm350_B23.Value = rm350_B23.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B23.Value = rm350_B23.Value - step_l;
                                    else rm350_B23.Value = rm350_B23.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[8], Convert.ToUInt16(rm350_R23.Value), mygamma_g[8], Convert.ToUInt16(rm350_G23.Value), mygamma_b[8], Convert.ToUInt16(rm350_B23.Value));
                        }
                        break;
                    case 20:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R19.Value = rm350_R19.Value - step_h;
                                    else rm350_R19.Value = rm350_R19.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R19.Value = rm350_R19.Value - step_l;
                                    else rm350_R19.Value = rm350_R19.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G19.Value = rm350_G19.Value - step_h;
                                    else rm350_G19.Value = rm350_G19.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G19.Value = rm350_G19.Value - step_l;
                                    else rm350_G19.Value = rm350_G19.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B19.Value = rm350_B19.Value - step_h;
                                    else rm350_B19.Value = rm350_B19.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B19.Value = rm350_B19.Value - step_l;
                                    else rm350_B19.Value = rm350_B19.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[7], Convert.ToUInt16(rm350_R19.Value), mygamma_g[7], Convert.ToUInt16(rm350_G19.Value), mygamma_b[7], Convert.ToUInt16(rm350_B19.Value));
                        }
                        break;
                    case 21:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R15.Value = rm350_R15.Value - step_h;
                                    else rm350_R15.Value = rm350_R15.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R15.Value = rm350_R15.Value - step_l;
                                    else rm350_R15.Value = rm350_R15.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G15.Value = rm350_G15.Value - step_h;
                                    else rm350_G15.Value = rm350_G15.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G15.Value = rm350_G15.Value - step_l;
                                    else rm350_G15.Value = rm350_G15.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B15.Value = rm350_B15.Value - step_h;
                                    else rm350_B15.Value = rm350_B15.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B15.Value = rm350_B15.Value - step_l;
                                    else rm350_B15.Value = rm350_B15.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[6], Convert.ToUInt16(rm350_R15.Value), mygamma_g[6], Convert.ToUInt16(rm350_G15.Value), mygamma_b[6], Convert.ToUInt16(rm350_B15.Value));
                        }
                        break;
                    case 22:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R11.Value = rm350_R11.Value - step_h;
                                    else rm350_R11.Value = rm350_R11.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R11.Value = rm350_R11.Value - step_l;
                                    else rm350_R11.Value = rm350_R11.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G11.Value = rm350_G11.Value - step_h;
                                    else rm350_G11.Value = rm350_G11.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G11.Value = rm350_G11.Value - step_l;
                                    else rm350_G11.Value = rm350_G11.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B11.Value = rm350_B11.Value - step_h;
                                    else rm350_B11.Value = rm350_B11.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B11.Value = rm350_B11.Value - step_l;
                                    else rm350_B11.Value = rm350_B11.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[5], Convert.ToUInt16(rm350_R11.Value), mygamma_g[5], Convert.ToUInt16(rm350_G11.Value), mygamma_b[5], Convert.ToUInt16(rm350_B11.Value));
                        }
                        break;
                    case 23:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R7.Value = rm350_R7.Value - step_h;
                                    else rm350_R7.Value = rm350_R7.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R7.Value = rm350_R7.Value - step_l * 3;
                                    else rm350_R7.Value = rm350_R7.Value + step_l * 3;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G7.Value = rm350_G7.Value - step_h;
                                    else rm350_G7.Value = rm350_G7.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G7.Value = rm350_G7.Value - step_l * 3;
                                    else rm350_G7.Value = rm350_G7.Value + step_l * 3;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B7.Value = rm350_B7.Value - step_h;
                                    else rm350_B7.Value = rm350_B7.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B7.Value = rm350_B7.Value - step_l * 3;
                                    else rm350_B7.Value = rm350_B7.Value + step_l * 3;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[4], Convert.ToUInt16(rm350_R7.Value), mygamma_g[4], Convert.ToUInt16(rm350_G7.Value), mygamma_b[4], Convert.ToUInt16(rm350_B7.Value));
                        }
                        break;
                    case 24://20阶以下
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R3.Value = rm350_R3.Value - step_h;
                                    else rm350_R3.Value = rm350_R3.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        rm350_R3.Value = rm350_R3.Value - step_l;
                                    else rm350_R3.Value = rm350_R3.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G3.Value = rm350_G3.Value - step_h;
                                    else rm350_G3.Value = rm350_G3.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        rm350_G3.Value = rm350_G3.Value - step_l;
                                    else rm350_G3.Value = rm350_G3.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B3.Value = rm350_B3.Value - step_h;
                                    else rm350_B3.Value = rm350_B3.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        rm350_B3.Value = rm350_B3.Value - step_l;
                                    else rm350_B3.Value = rm350_B3.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(mygamma_r[3], Convert.ToUInt16(rm350_R3.Value), mygamma_g[3], Convert.ToUInt16(rm350_G3.Value), mygamma_b[3], Convert.ToUInt16(rm350_B3.Value));
                        }
                        break;

                }

            }
        }


        [DllImport("user32.dll")]
        public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time);//引用DLL
        private void R66451_btnPrevPic_Run(string filenames)       //R66451
        {
            Int32 HACT_True = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            try
            {
                //string FileName = textBox2.Text + '\' + listBox3_BMPFile.SelectedItem.ToString();
                string Filename;
                if (SK_102_Check.Checked == true)
                {

                    Filename = System.Windows.Forms.Application.StartupPath + "\\Resources\\SK_102\\" + filenames;

                }
                else
                {
                    Filename = System.Windows.Forms.Application.StartupPath + "\\Resources\\Alpha\\" + filenames;

                }
                FileStream aFile = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        m_txlen += ByteReaded;
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label14.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        MainForm.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }

                    aFile.Close();
                }
                /*
                else
                {
                    MessageBox.Show("BMP分辨率非法");
                }*/
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }
        private void Send_Cmd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.Name)
            {
                case "btnSend1":
                    {
                        MainForm.txtProcessDrivercode(txtCmd1.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend2":
                    {
                        MainForm.txtProcessDrivercode(txtCmd2.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend3":
                    {
                        MainForm.txtProcessDrivercode(txtCmd3.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend4":
                    {
                        MainForm.txtProcessDrivercode(txtCmd4.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;

            }
            Setting.TempRecord.cmdLen = 6;
            if (Write_LP.Checked)
            {
                MainForm.SendCmd(0);
            }
            else
            {
                MainForm.SendCmd(4);
            }
            textBox1.Text += "命令已下发...\r\n";
            textBox1.Select(textBox1.Text.Length, 0);
            textBox1.ScrollToCaret();
        }
        private void OpenTxtCode_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "初始化 Code";

            if (of.ShowDialog() == DialogResult.OK)
            {
                StreamReader streamReader = new StreamReader(of.FileName, Encoding.Default);
                switch (btn.Name)
                {
                    case "OpenTxtCode":
                        {
                            txtCmd1.Clear();
                            txtCmd1.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode2":
                        {
                            txtCmd2.Clear();
                            txtCmd2.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode3":
                        {
                            txtCmd3.Clear();
                            txtCmd3.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode4":
                        {
                            txtCmd4.Clear();
                            txtCmd4.Text = streamReader.ReadToEnd();
                        }
                        break;
                    default: break;
                }
                streamReader.Close();
            }
        }
        private void btnSaveDriverCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            //sfd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
            sfd.Title = "请选择要保存的文件路径";
            sfd.Filter = "文本文件|*.txt|所有文件|*.*";
            sfd.ShowDialog();

            //获得用户要保存的文件的路径
            string path = sfd.FileName;
            if (path == "")
            {
                return;
            }
            using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] buffer = Encoding.Default.GetBytes(txtCmd1.Text);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功");
        }
        private void SaveCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog txt = new SaveFileDialog();
            txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            string str = "//-----保存Driver IC Code文件------\r\n";
            if (txt.ShowDialog() == DialogResult.OK)
            {
                if (txt.FileName == " ")
                {
                    return;
                }
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(txt.FileName, false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
                try
                {
                    if (ChkCMD1.Checked)
                    {
                        str += "//---CMD1---\r\n" + txtCmd1.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD2.Checked)
                    {
                        str += "//---CMD2---\r\n" + txtCmd2.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD3.Checked)
                    {
                        str += "//---CMD3---\r\n" + txtCmd3.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD4.Checked)
                    {
                        str += "//---CMD4---\r\n" + txtCmd4.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }

                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Driver IC Code保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Driver IC Code保存时发生错误，请再试！", "系统提示：");
                }
            }

        }
        public static bool Delay(int delayTime)
        {
            DateTime now = DateTime.Now;
            int s;
            do
            {
                TimeSpan spand = DateTime.Now - now;
                s = spand.Seconds;
                Application.DoEvents();
            }
            while (s < delayTime);
            return true;
        }
        private void PreLoad_Demura_data_Click()  //预加载Demura文件//private void PreLoad_Demura_data_Click(object sender, EventArgs e)  //预加载Demura文件
        {
            Int32 m_txlen = 0;
            Int32 Once_txlen = 0;
            Int32 File_Length, File_Length_tep;
            bool tx_en = true;
            byte[] TemFileContent = new byte[1024 * 100];
            char[] Acsii_array = new char[2];
            Int32 div = 0;

            div = Convert.ToInt16(MainForm.HACT.Value) * 3;
            System.Int32 ByteReaded = 0x00;
            try
            {
                string FileName = textBox5.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                File_Length = Convert.ToInt32(aFile.Length);
                File_Length_tep = File_Length;

                while (tx_en)
                {
                    if (File_Length_tep > (File_Length % div))
                    {
                        Once_txlen = div;
                    }
                    else  //最后一笔
                    {
                        Once_txlen = Convert.ToInt32(aFile.Length % div);
                        tx_en = false;
                    }
                    aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
                    ByteReaded = aFile.Read(TemFileContent, 5, Once_txlen); // 存入第五位
                    try
                    {
                        TemFileContent[0] = 0x5B;
                        TemFileContent[1] = Convert.ToByte(File_Length >> 24);
                        TemFileContent[2] = Convert.ToByte((File_Length >> 16) & 0xff);
                        TemFileContent[3] = Convert.ToByte((File_Length >> 8) & 0xff);
                        TemFileContent[4] = Convert.ToByte(File_Length & 0xff);
                        MainForm.comPort.Write(TemFileContent, 0, ByteReaded + 5);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        break;
                    }
                    //tx_en = false;
                    m_txlen += ByteReaded;
                    progressBar2.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                    label_flash.Text = progressBar2.Value.ToString() + "%";
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    File_Length_tep = File_Length_tep - div;
                }
                aFile.Close();
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }





        void auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[17];
            Setting.TempRecord.cmdLen = 17;

            if ((datar < 0x000f) || (datag < 0x000f) || (Datab < 0x000f) || (datar > 0x0ff0) || (datag > 0x0ff0) || (Datab > 0x0ff0))
            {

                T17.Enabled = false;

                flag1 = 1;
                flag2 = 1;
                flag3 = 1;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                MessageBox.Show("数值超出界限，已停止gamma调试", "提示");
            }



            Send_buf[0] = 0x59;//350


            Send_buf[1] = 0xDD;
            Send_buf[2] = 12;
            Send_buf[3] = Convert.ToByte(addr >> 8);
            Send_buf[4] = Convert.ToByte(datar >> 8);
            Send_buf[5] = Convert.ToByte(addr & 0xff);
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(addg >> 8);
            Send_buf[8] = Convert.ToByte(datag >> 8);
            Send_buf[9] = Convert.ToByte(addg & 0xff);
            Send_buf[10] = Convert.ToByte(datag & 0xff);
            Send_buf[11] = Convert.ToByte(addb >> 8);
            Send_buf[12] = Convert.ToByte(Datab >> 8);
            Send_buf[13] = Convert.ToByte(addb & 0xff);
            Send_buf[14] = Convert.ToByte(Datab & 0xff);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15], out Send_buf[16]);
            try { MainForm.comPort.Write(Send_buf, 0, 17); }

            catch { MessageBox.Show("串口有误，请检查", "错误"); }
        }











    }
}
